package com.java.forum.c_service;

import com.java.forum.a_entity.PLike;
import com.java.forum.a_entity.Post;
import com.java.forum.b_mapper.PLikeMapper;
import com.java.forum.b_mapper.PostMapper;
import com.java.forum.g_exception.Exceptions;
import com.java.forum.g_exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class PostServiceImpl implements PostService {
    private PostMapper postMapper;
    private PLikeMapper plikeMapper;


    @Autowired
    public void setPostMapper(PostMapper postMapper) {
        this.postMapper = postMapper;
    }

    @Autowired
    public void setPLikeMapper(PLikeMapper plikeMapper) {
        this.plikeMapper = plikeMapper;
    }

    @Override
    public Integer publishPost(Post post) {
        post.setComments(0);
        post.setLikes(0);
        post.setViews(0);
        post.setCollections(0);
        post.setDate(new Date());
        Integer res = postMapper.insertPost(post);
        if (res != 1) throw new ServiceException(Exceptions.PostInsertException);
        return post.getPid();
    }

    @Override
    public Post getPostInfo(Integer pid) {
        Post post = postMapper.getPostByPid(pid);
        if (post == null) throw new ServiceException(Exceptions.PostNotFound);
        postMapper.ViewPost(post);
        return post;
    }

    @Override
    public Integer ReEditPost(Post post) {
        Integer res = postMapper.ReEditPost(post);
        if (res != 1) throw new ServiceException(Exceptions.PostReEditErr);
        return post.getPid();
    }

    @Override
    public List<Post> getPostByKeyword(String keyword, Date date) {
        List<Post> posts = postMapper.getPostByKeyword(keyword, date);
        if (posts.size() == 0) throw new ServiceException(Exceptions.PostSearchNone);
        return posts;
    }

    @Override
    public List<Post> getPostByType(Integer type, Date date) {
        List<Post> posts = postMapper.getPostByType(type, date);
        if (posts.size() == 0) throw new ServiceException(Exceptions.PostSearchNone);
        return posts;
    }

    @Override
    public List<Post> getAllPost(Date date) {
        List<Post> posts = postMapper.getAllPost(date);
        if (posts.size() == 0) throw new ServiceException(Exceptions.PostSearchNone);
        return posts;
    }

    @Override
    public List<Post> getAllPostOrderByComments(Date date) {
        List<Post> posts = postMapper.getAllPostOrderByComments(date);
        if (posts.size() == 0) throw new ServiceException(Exceptions.PostSearchNone);
        return posts;
    }

    @Override
    public List<Post> getAllPostOrderByLikes(Date date) {
        List<Post> posts = postMapper.getAllPostOrderByLikes(date);
        if (posts.size() == 0) throw new ServiceException(Exceptions.PostSearchNone);
        return posts;
    }

    @Override
    public List<Post> getAllPostOrderByViews(Date date) {
        List<Post> posts = postMapper.getAllPostOrderByViews(date);
        if (posts.size() == 0) throw new ServiceException(Exceptions.PostSearchNone);
        return posts;
    }

    @Override
    public List<Post> getPostByUid(Integer uid, Date date) {
        return postMapper.getPostByUid(uid, date);
    }

    @Override
    public Integer likePost(PLike plike) {
        if (plikeMapper.IsLiked(plike.getPid(),plike.getUid()) != null){
            throw new ServiceException(Exceptions.PostHaveBeenLikedException);
        }
        Integer res = plikeMapper.InsertLiked(plike);
        if (res != 1) throw new ServiceException(Exceptions.PLikeInsertException);
        Post post = postMapper.getPostByPid(plike.getPid());
        Integer res1 = postMapper.PostLike(post);
        if (res1 != 1) throw new ServiceException(Exceptions.PLikeInsertException);
        return plike.getId();
    }

    @Override
    public void UnLikePost(PLike plike) {
        Integer res = plikeMapper.DeleteLiked(plike);
        if (res < 1) throw new ServiceException(Exceptions.PLikeDeleteException);
        Post post = postMapper.getPostByPid(plike.getPid());
        Integer res1 = postMapper.PostUnlike(post);
        if (res1 != 1) throw new ServiceException(Exceptions.PLikeInsertException);
    }

    @Override
    public Integer isLiked(int pid, int uid) {
        PLike pLike = plikeMapper.IsLiked(pid, uid);
        if (pLike == null) {
            return 0;
        }
        return 1;
    }

    @Override
    public Integer deletePost(Post post){
        Integer res1 = postMapper.deletePost(post);
        Integer res2 = postMapper.deletePostThenLike(post);
        Integer res3 = postMapper.deletePostThenCollection(post);
        Integer res4 = postMapper.deletePostThenComment(post);
        return 1;
    }

    @Override
    public Integer reCounterScore(Post post) {
        return postMapper.reCounterScore(post);
    }
}
